package org.lc.stk.dao.bank;

import java.util.List;

import org.lc.stk.model.banks.CourseQuestionBank;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CourseQuestionBankRepository extends JpaRepository<CourseQuestionBank, Integer> {
    
    /**
     * 根据课程ID查询题库列表
     */
    List<CourseQuestionBank> findByCourseId(Integer courseId);

    /**
     * 检查名称在指定课程下是否存在
     */
    @Query("SELECT COUNT(b) > 0 FROM CourseQuestionBank b WHERE b.name = :name AND b.course.id = :courseId AND (:excludeId IS NULL OR b.id != :excludeId)")
    boolean existsByNameAndCourseIdAndIdNot(
        @Param("name") String name, 
        @Param("courseId") Integer courseId,
        @Param("excludeId") Integer excludeId
    );

    /**
     * 分页查询
     */
    @Query("SELECT b FROM CourseQuestionBank b WHERE " +
           "(:courseId IS NULL OR b.course.id = :courseId) AND " +
           "(:name IS NULL OR b.name LIKE %:name%) AND " +
           "(:state IS NULL OR b.state = :state)")
    Page<CourseQuestionBank> findByConditions(
        @Param("courseId") Integer courseId,
        @Param("name") String name,
        @Param("state") Integer state,
        Pageable pageable
    );
}